package exhibition.app.nexd.cn.widget;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import exhibition.app.nexd.cn.R;
import exhibition.app.nexd.cn.widget.wheelwidget.WheelView;
import exhibition.app.nexd.cn.widget.wheelwidget.adapters.ArrayWheelAdapter;


public class AddSubPopupWindow extends PopupWindow implements OnClickListener {

    private Context context;
    private View addsubLayout;
    private ImageView bgImageView;
    private TextView tvTitle;
    private WheelView wlAddSub;
    private EditText editInputNum;
    private Button btnCommit;
    private Button btnCancle;
    private int type;
    private String[] itemData;
    public static final int TYPE_ADD_DAY = 1;
    public static final int TYPE_ADD_NUM = 2;
    public static final int TYPE_SUB_DAY = 3;
    public static final int TYPE_SUB_NUM = 4;
    private OnSubmitClickListener onSubmitClickListener;
    private View hireRlEdit;


    public AddSubPopupWindow(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public void setOnSubmitClickListener(OnSubmitClickListener onSubmitClickListener) {
        this.onSubmitClickListener = onSubmitClickListener;
    }


    private void init() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        addsubLayout = inflater.inflate(R.layout.dialog_hire_add, null);
        hireRlEdit = addsubLayout.findViewById(R.id.hire_rl_day_num);
        bgImageView = (ImageView) addsubLayout.findViewById(R.id.hire_bg_exit);
        tvTitle = (TextView) addsubLayout.findViewById(R.id.hire_add_sub_title);
        wlAddSub = (WheelView) addsubLayout.findViewById(R.id.hire_wl_day_num);
        editInputNum = (EditText) addsubLayout.findViewById(R.id.hire_edit_day_num);
        btnCommit = (Button) addsubLayout.findViewById(R.id.hire_btn_commit);
        btnCancle = (Button) addsubLayout.findViewById(R.id.hire_btn_cancle);
        btnCommit.setOnClickListener(this);
        btnCancle.setOnClickListener(this);
        //initWheelView();
        // 设置PopupWindow的View
        this.setContentView(addsubLayout);
        // 设置PopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        // 设置PopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        // 设置PopupWindow弹出窗体可点击
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(Color.parseColor("#00000000"));
        // 设置PopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
    }

    private void initWheelView() {
        wlAddSub.setViewAdapter(new ArrayWheelAdapter<String>(context, itemData));
        wlAddSub.setVisibleItems(6);
    }


    public void setItemData(int maxLength, int currentItem) {
        if (maxLength > 10) {
            hireRlEdit.setVisibility(View.VISIBLE);
            editInputNum.setText(currentItem + "");
            wlAddSub.setVisibility(View.GONE);
        } else {
            editInputNum.setVisibility(View.GONE);
            wlAddSub.setVisibility(View.VISIBLE);
            itemData = new String[maxLength];
            for (int i = 0; i < maxLength; i++) {
                itemData[i] = (i + 1) + "";
            }
            wlAddSub.setCurrentItem(currentItem);
            initWheelView();
        }
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        bgImageView.setVisibility(View.VISIBLE);
        PropertyValuesHolder alphaPV = PropertyValuesHolder.ofFloat("alpha", 0, 0.5f);
        // 整个背景
        ObjectAnimator.ofPropertyValuesHolder(bgImageView, alphaPV).setDuration(400).start();

    }

    @Override
    public void dismiss() {
        PropertyValuesHolder alphaPV = PropertyValuesHolder.ofFloat("alpha", 0.5f, 0);
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(bgImageView, alphaPV).setDuration(
                400);
        animator.addListener(new Animator.AnimatorListener() {

            @Override
            public void onAnimationStart(Animator arg0) {

            }

            @Override
            public void onAnimationRepeat(Animator arg0) {

            }

            @Override
            public void onAnimationEnd(Animator arg0) {
                AddSubPopupWindow.this.hide();
            }

            @Override
            public void onAnimationCancel(Animator arg0) {

            }
        });
        animator.start();
    }

    private void hide() {
        super.dismiss();
    }

    public void setType(int type) {
        switch (type) {
            case TYPE_ADD_DAY:
                tvTitle.setText("天数");
                break;
            case TYPE_SUB_DAY:
                tvTitle.setText("天数");
                break;
            case TYPE_ADD_NUM:
                tvTitle.setText("数量");
                break;
            case TYPE_SUB_NUM:
                tvTitle.setText("数量");
                break;
        }
        this.type = type;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.hire_btn_cancle:
                dismiss();
                break;
            case R.id.hire_btn_commit:
                if (onSubmitClickListener != null) {
                    if (editInputNum.getVisibility() == View.GONE) {
                        onSubmitClickListener.onSubmit(type, itemData[wlAddSub.getCurrentItem()]);
                    } else if (editInputNum.getVisibility() == View.VISIBLE) {
                        onSubmitClickListener.onSubmit(type, editInputNum.getText().toString());
                    }
                    dismiss();
                }
                break;
        }
    }


    public interface OnSubmitClickListener {
        void onSubmit(int type, String num);
    }


}




